package com.why.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.why.entity.Account;
import com.why.entity.Result;
import com.why.entity.SerachAc;
import com.why.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴慧跃
 * @since 2022-05-27
 */
@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    IAccountService accountService;

    @RequestMapping("/updateAc")
    public Result<String> updateAc(@RequestBody Account account){
        if (account.getBalance()==null){
            account.setBalance(account.getMoneyUser());
        }else {
            account.setBalance(account.getBalance()+account.getMoneyUser());
        }
        boolean b = accountService.updateById(account);
        if (b){
            return new Result<String>(200,"","修改成功");
        }else {
            return new Result<String>(400,"","修改失败");
        }
    }

    @RequestMapping("/updateAc2")
    public Result<String> updateAc2(@RequestBody Account account){
        account.setBalance((account.getBalance()-account.getMoneyUser()));
        boolean b = accountService.updateById(account);
        if (b){
            return new Result<String>(200,"","修改成功");
        }else {
            return new Result<String>(400,"","修改失败");
        }
    }

    @RequestMapping("/addAc")
    public Result<String> addAc(@RequestBody Account account){
        boolean b = accountService.saveOrUpdate(account);
        if (b){
            return new Result<String>(200,"","添加成功");
        }else {
            return new Result<String>(400,"","添加失败");
        }
    }

    @RequestMapping("/getAccount")
    public Result<Account> getAccount(int id){
        Account account = accountService.getById(id);
        return new Result<Account>(200,"",account);
    }

    @RequestMapping("/listAccount")
    public Page<Account> listAccount(@RequestBody SerachAc serachAc){
        Page<Account> page = new Page<>(serachAc.getPage(), serachAc.getPageSize());
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        // 姓名模糊查
        if (StringUtils.hasText(serachAc.getName())){
            wrapper.like("name",serachAc.getName());
        }
        // 账号查询
        if (serachAc.getNumber()!=null){
            wrapper.eq("number",serachAc.getNumber());
        }
        // 开户区间
        if (serachAc.getBeginTimeStart()!=null){
            wrapper.gt("begin_time",serachAc.getBeginTimeStart());
        }
        if (serachAc.getBeginTimeEnd()!=null){
            wrapper.lt("begin_time",serachAc.getBeginTimeEnd());
        }
        // 余额区间
        if (serachAc.getBalanceStart()!=null){
            wrapper.gt("balance",serachAc.getBalanceStart());
        }
        if (serachAc.getBalanceEnd()!=null){
            wrapper.lt("balance",serachAc.getBalanceEnd());
        }
        Page<Account> page1 = accountService.page(page, wrapper);
        return page1;
    }
}
